package com.xopencode.multithread;

import java.util.concurrent.*;

/**
 * @author Mcb
 * @date 2022/5/23
 * @apiNote 线程池提交
 */
public class ThreadPool {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        /*ExecutorService threadPool = Executors.newSingleThreadExecutor();//单个线程
        ExecutorService threadPool2 = Executors.newFixedThreadPool(5); //创建一个固定的线程池的大小
        ExecutorService threadPool3 = Executors.newCachedThreadPool(); //可伸缩的*/

        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
                2,
                10,
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(10),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()
        );

        poolExecutor.execute(()->{
            System.out.println(Thread.currentThread().getName()+ " ok");
        });

        Future<String> future = poolExecutor.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("Call:" + Thread.currentThread().getName());
                return "String" + Thread.currentThread().getName();
            }
        });
        //get方法堵塞
        String result = future.get();
        System.out.println("结果：" + result);
    }
}
